import { login, logout, getInfo } from "@/api/login";
import { getToken, setToken, removeToken } from "@/utils/auth";
import defAva from "@/assets/images/profile.jpg";
import { defineStore } from "pinia";
import { IUserInfo, role } from "./type/index";
import { initTableConfig } from "@/utils/tableConfig";

const useUserStore = defineStore("user", {
    state: () => ({
        token: getToken(),
        name: "",
        avatar: "",
        roles: [] as string[],
        permissions: [],
        rolePermissions: {},
        user: {},
        role: (<role>{}) as role, //当前功能选中的角色
        mode: "multiple", //角色模式(multiple：多角色模式，前端会弹出角色选择,single：单角色模式，把多个角色权限进行合并);默认为multiple
    }),
    actions: {
        // 登录
        login(userInfo: IUserInfo) {
            const username = userInfo.username.trim();
            const password = userInfo.password;
            const code = userInfo.code;
            const uuid = userInfo.uuid;
            return new Promise<void>((resolve, reject) => {
                login(username, password, code, uuid)
                    .then((res) => {
                        const data = res.data;
                        setToken(data.access_token);
                        this.token = data.access_token;
                        resolve();
                    })
                    .catch((error) => {
                        reject(error);
                    });
            });
        },
        // 获取用户信息
        getInfo() {
            return new Promise((resolve, reject) => {
                getInfo()
                    .then((res: any) => {
                        const user = res.user;
                        const avatar = user.avatar == "" || user.avatar == null ? defAva : user.avatar;

                        if (res.roles && res.roles.length > 0) {
                            // 验证返回的roles是否是一个非空数组
                            this.roles = res.roles;
                            this.permissions = res.permissions;
                            this.rolePermissions = res.rolePermissions;
                        } else {
                            this.roles = ["ROLE_DEFAULT"];
                        }
                        this.name = user.userName;
                        this.avatar = avatar;
                        this.user = user;
                        this.mode = res.mode;
                        //初始化表格配置
                        // initTableConfig({ roles: this.roles });
                        resolve(res);
                    })
                    .catch((error) => {
                        reject(error);
                    });
            });
        },
        // 退出系统
        logOut() {
            return new Promise<void>((resolve, reject) => {
                logout()
                    .then(() => {
                        this.token = "";
                        this.roles = [];
                        this.permissions = [];
                        removeToken();
                        resolve();
                    })
                    .catch((error) => {
                        reject(error);
                    });
            });
        },
        //设置选中的角色
        setRole(role: any) {
            this.role = role;
        },
        getRole() {
            return this.role;
        },
    },
});

export default useUserStore;
